package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 中国江西网
 * @author Persh
 *
 */
public class JxcnNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://[a-z]*.jxcn.cn/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*.shtml",
		"http://[a-z]*.jxcn.cn/[0-9]*/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/.*.htm"
	};
	
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}				
		news.setUrl(url);		
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("div.title").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("h1").text();
			if(StringUtils.isBlank(title)){
				title =doc.select("td.title").text();
				if(StringUtils.isBlank(title)){
					title =doc.select("span.p22Bold").text();
					if(StringUtils.isBlank(title)){
						title =doc.select("td.dbtp1").text();
					}
				}
			}
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("font#Zoom"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#fontzoom"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("td.p14"));
			}
		}
		
		/*
		 * 作者
		 */
		String authorStr=null;
		authorStr=doc.select("div.biaoti1 div.left").text();
		if(StringUtils.isBlank(authorStr)){
			authorStr=doc.select("td[height=24]").text();
			if(StringUtils.isBlank(authorStr)){
				authorStr=doc.select("td.p12").text();
				if(StringUtils.isBlank(authorStr)){
					authorStr=doc.select("div.date").text();
				}
			}
		}
		if(authorStr.indexOf("来源：") != -1){
			author=StringUtils.substringBetween(authorStr, "来源:", " ");
		}
		else{
			author=StringUtils.substringBetween(authorStr, "    ","  ");
		}
	
		date=DateUtils.matchDate(doc.select("div.left").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("td[height=24]").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("td.p12").text());
				if(date==null){
					date=DateUtils.matchDate(doc.select("div.date").text());
				}
			}
		}
		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);	
		
		return news;
	}
	
	public static void main(String[] args) {
		JxcnNewsAnalyse analyse=new JxcnNewsAnalyse();
		String url="http://huanbao.jxcn.cn/system/2013/04/01/012353470.shtml";
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
